image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay2
  CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

  IMAGE_APP: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA-app
  IMAGE_APP_STAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA-stage-app
  IMAGE_APP_PROD: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA-prod-app

  IMAGE_STATIC: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA-static

  SERVICE_NAME: companyName_site
  SERVICE_NAME_STAGE: companyName_site
  SERVICE_NAME_PROD: companyName_site_prod

stages:
  - build
  - deploy

.login: &login docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY

#----------------------------DEV(main)----------------------------#

.main-env: &main-environment
  environment:
    name: companyName_site
    url: https://dev.companyName.fmake.ru/

.main-tags: &main-tags
  tags:
    - companyName_server_dev

.build-app-main: &build-app-main
  stage: build
  artifacts:
    paths:
      - .deploy
  variables:
    GIT_STRATEGY: clone
  before_script:
    - *login
    - mv ./public/.well-known/dev/** ./public/.well-known
    - rm -rf ./public/.well-known/prod

  script:
    - docker build -f .deploy/main/Dockerfile --target main --tag "$IMAGE_APP" .
    - docker push "$IMAGE_APP"

build-main:
  <<: *build-app-main
  <<: *main-tags
  only:
    refs:
      - main

.deploy-app-main: &deploy-main
  stage: deploy
  variables:
    GIT_STRATEGY: none
  before_script:
    - *login
    - docker pull "$IMAGE_APP"

deploy-main:
  <<: *deploy-main
  <<: *main-environment
  <<: *main-tags
  script:
    - docker stack deploy --with-registry-auth --compose-file=.deploy/main/release-compose.yml "$SERVICE_NAME"
  needs: ['build-main']
  only:
    refs:
      - main

#----------------------------STAGE----------------------------#

.stage-env: &stage-environment
  environment:
    name: companyName_site_stage
    url: https://prod.companyName.ru/

.stage-tags: &stage-tags
  tags:
    - companyName_server_prod

.build-app-stage: &build-app-stage
  stage: build
  artifacts:
    paths:
      - .deploy
  variables:
    GIT_STRATEGY: clone
  before_script:
    - *login
  script:
    - docker build -f .deploy/stage/Dockerfile --target stage --tag "$IMAGE_APP_STAGE" .
    - docker push "$IMAGE_APP_STAGE"

build-stage:
  <<: *build-app-stage
  <<: *stage-tags
  only:
    refs:
      - stage

.deploy-app-stage: &deploy-stage
  stage: deploy
  variables:
    GIT_STRATEGY: none
  before_script:
    - *login
    - docker pull "$IMAGE_APP_STAGE"

deploy-stage:
  <<: *deploy-stage
  <<: *stage-environment
  <<: *stage-tags
  script:
    - docker stack deploy --with-registry-auth --compose-file=.deploy/stage/release-compose.yml "$SERVICE_NAME_STAGE"
  needs: ['build-stage']
  only:
    refs:
      - stage

#----------------------------PROD----------------------------#

.production-env: &production-environment
  environment:
    name: companyName_site_prod
    url: https://companyName.ru/

.production-tags: &production-tags
  tags:
    - companyName_server_prod

.build-app-production: &build-app-production
  stage: build
  artifacts:
    paths:
      - .deploy
  variables:
    GIT_STRATEGY: clone
  before_script:
    - *login
    - cp ./public/.well-known/prod/** ./public/.well-known
    - rm -rf ./public/.well-known/dev
  script:
    - docker build -f .deploy/production/Dockerfile --target production --tag "$IMAGE_APP_PROD" .
    - docker push "$IMAGE_APP_PROD"

build-production:
  <<: *build-app-production
  <<: *production-tags
  only:
    refs:
      - production

.deploy-app-prod: &deploy-prod
  stage: deploy
  variables:
    GIT_STRATEGY: none
  before_script:
    - *login
    - docker pull "$IMAGE_APP_PROD"

deploy-production:
  <<: *deploy-prod
  <<: *production-environment
  <<: *production-tags
  script:
    - docker stack deploy --with-registry-auth --compose-file=.deploy/production/release-compose.yml "$SERVICE_NAME_PROD"
  needs: ['build-production']
  only:
    refs:
      - production
